package com.nine.system.controller;

import cn.hutool.core.util.IdUtil;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.nine.common.pojo.User;
import com.nine.common.vo.Result;
import com.nine.system.service.UserService;
import com.nine.system.vo.LoginVo;
import com.nine.system.vo.UserVo;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.concurrent.TimeUnit;

/**
 * @program: nine-education-parent
 * @author: ♥丁新华
 * @create: 2023-05-23 10:24
 **/
@RestController
@RequestMapping("/system/user")
public class UserController {

    @Autowired
    private StringRedisTemplate redisTemplate;

    @Autowired
    private UserService userService;
    @PostMapping ("/login")
    @ApiOperation (value = "登录接口")
    public Result findByNameAndPwd(@RequestBody LoginVo loginVo){
        User user = userService.findByNameAndPwd (loginVo.getUsername (), loginVo.getPassword ());

        if (user != null){
        //判断是否存在
        if (user.getIsDeleted ().equals (0)){
            //表示成功了
            //生成  token  令牌
            String token = IdUtil.simpleUUID ();
            System.out.println ("++++++++++---------" + token);
            //设 置 为 空
            user.setPassword (null);

            redisTemplate.opsForValue ().set ("user::"+token, JSON.toJSONString(user),30, TimeUnit.MINUTES);

            return Result.success (token);

        }else {
            //这种情况 表示账号被冻结
            return Result.fail ("账号被冻结，联系管理员");
        }
      }
        return Result.fail ("账号或者密码错误");

    }

    @GetMapping("/info")
    @ApiOperation(value = "获取当前用户信息")
    public Result info(HttpServletRequest request){
        // 从请求头获取token信息
        String token = request.getHeader("token");
        // 从redis数据库  中获取当前登录者的信息
        String json = redisTemplate.opsForValue().get("user::" + token);

        User user = JSON.parseObject(json, User.class);
        return Result.success(user);
    }

    @GetMapping("/logout")
    @ApiOperation(value = "退出登录")
    public Result logout(HttpServletRequest request){
        // 从请求头获取token信息
        String token = request.getHeader("token");

        //删除数据
        Boolean delete = redisTemplate.delete("user::" + token);
        return Result.success();
    }


    @PostMapping("/page/{current}/{pageSize}")
    @ApiOperation (value = "分页查询")
    public Result findUserPage(@ApiParam(name = "current",value = "当前页数" ,required = false,defaultValue = "1")
                               @PathVariable Integer current,
                               @ApiParam(name = "pageSize",value = "每页条数" ,required = false,defaultValue = "3")
                               @PathVariable Integer pageSize,
                               @RequestBody UserVo userVo){

        Page<User> userPage = userService.findUserPage (current, pageSize, userVo);


        return Result.success (userPage);

    }

    /**
     * 修改状态
     */
    @GetMapping("/status")
    @ApiOperation (value = "修改用户的状态")
    public Result statusModify(@RequestParam String id, @RequestParam Integer status){
        int row=userService.updateStatus(id,status);
        return Result.success();
    }

}

